package org.nicocube.airain.domain.client.gamedate;

import java.io.Serializable;

import javax.jdo.annotations.Column;
import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class GameDateManager implements Serializable {

	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long order;
	
	@Persistent
	@Embedded(members={@Persistent(name="timestamp",columns=@Column(name="beginning"))})
	private GameDate beginning;

	@Persistent
	@Embedded(members={@Persistent(name="timestamp",columns=@Column(name="last"))})
	private GameDate last;
	
	@Persistent
	@Embedded(members={@Persistent(name="timestamp",columns=@Column(name="current"))})
	private GameDate current;
	
	// for GWT use only
	protected GameDateManager() {}
	
	public GameDateManager(GameDate beginning) {
		this.beginning = beginning;
		IncrementalGameDate last = new IncrementalGameDate(beginning).incHour(-1);
		this.last = new GameDate(last);
		this.current = new GameDate(beginning);
	}
	
	public Long getOrder() {
		return order;
	}
	
	public GameDate getBeginning() {
		return beginning;
	}
	public GameDate getLast() {
		return last;
	}
	public GameDate getCurrent() {
		return current;
	}
	public GameDate getNext() {
		return new GameDate(current.getHours()+1,current.getMoonDays(),current.getYearMoons(),current.getYears());
	}
	public void incCurrent() {
		last=current;
		current=getNext();
	}

}
